var url = "/openx/logistics/logisticsDeliveryManager/queryDeliveryForPage";
var queryForm = 'queryForm';
var ownOptions={};
//对应接口入参
var param = {};
(function () {
//        /**设置增删改查的工具条*/
    $.openx.setTableToolBar('manageBar');
    /**进行表格数据初始化*/
    var data = $.openx.getJsonObj(queryForm);
    param.bean = data;
    $.openx.initTable("table", url, param);
    initAreaSelect("province", "city", "district");
    initAreaSelect("receive_province", "receive_city", "receive_district");
    initShield("deliveryType");
})();

function initShield(second){
    $X("/logistics/logisticsShield", "queryAllShieldType").callx({
        onResult: function (data) {
            if (data) {
                bindSelectWithData(second,data);
            } else {
                layer.msg("无信息")
            }
        },
        onError: function (error) {
            var res = JSON.parse(error);
            layer.msg(res.message)
        }
    });
}

function initOrg(second){
    $X("/logistics/logisticsDeliveryManager", "queryAllOrg").callx({
        onResult: function (data) {
            if (data) {
                bindSelectWithData(second,data);
            } else {
                layer.msg("无信息")
            }
        },
        onError: function (error) {
            var res = JSON.parse(error);
            layer.msg(res.message)
        }
    });
}


$.openx.insert = function () {

    var createForm = "addManagerForm";
    var innerHtml = $.openx.innerHtml('addManagerDialog');
    laytpl(innerHtml).render({}, function (html) {
        $.openx.openLayer(['800', '400'], '新增物流发货管理', html, false, function () {
            if ($.openx.validation(createForm)) {
                //获取表单数据
                // var topChannelName = $("#add_channel option:selected").text();
                // $("#add_channelId").val(topChannelName)
                $("#deliveryAreaCode").val($("#add_city option:selected").val());

                var deliveryProvinceName = $("#add_province option:selected").text();
                $("#add_provinceId").val(deliveryProvinceName)
                var deliveryCityName = $("#add_city option:selected").text();
                $("#add_cityId").val(deliveryCityName)

                var receiveProvinceName = $("#receive_add_province option:selected").text();
                $("#add_receive_provinceId").val(receiveProvinceName)
                var receiveCityName = $("#receive_add_city option:selected").text();
                $("#add_receive_cityId").val(receiveCityName)
                var receiveDistrictName = $("#receive_add_district option:selected").text();
                $("#add_receive_districtId").val(receiveDistrictName)

                var param = $.openx.getJsonObj(createForm);
                $X("/boss/logistics/logisticsDeliveryManager", "createDelivery").callx({
                    param: param,
                    onResult: function (data) {
                        if (data) {
                            $.openx.tableRefresh(null, null, true);
                            layer.closeAll();
                            layer.msg("新增成功");
                        }
                    },
                    onError: function (error) {
                        var res = JSON.parse(error);
                        layer.msg(res.message)
                    }
                });
            }
        });
    });
    initAreaSelect("add_province", "add_city", "district");
    initAreaSelect("receive_add_province", "receive_add_city", "receive_add_district");
    initShield("add_deliveryType");
    initOrg("add_partner");
}



$.openx.update = function () {
    $.openx.onClickUpdate(function (row) {
        var updateForm = "addManagerForm";
        var innerHtml = $.openx.innerHtml('addManagerDialog');
        laytpl(innerHtml).render(row[0], function (html) {
            $.openx.openLayer(['800', '400'], '修改物流发货信息', html, false, function () {
                if ($.openx.validation(updateForm)) {
                    //获取表单数据
                    $("#deliveryAreaCode").val($("#add_city option:selected").val());

                    var deliveryProvinceName = $("#add_province option:selected").text();
                    $("#add_provinceId").val(deliveryProvinceName)
                    var deliveryCityName = $("#add_city option:selected").text();
                    $("#add_cityId").val(deliveryCityName)

                    var receiveProvinceName = $("#receive_add_province option:selected").text();
                    $("#add_receive_provinceId").val(receiveProvinceName)
                    var receiveCityName = $("#receive_add_city option:selected").text();
                    $("#add_receive_cityId").val(receiveCityName)
                    var receiveDistrictName = $("#receive_add_district option:selected").text();
                    $("#add_receive_districtId").val(receiveDistrictName)


                    var param = $.openx.getJsonObj(updateForm);
                    $X("/logistics/logisticsDeliveryManager", "updateDelivery").callx({
                        param: param,
                        onResult: function (data) {
                            if (data) {
                                $.openx.tableRefresh(null, null, true);
                                layer.closeAll();
                                layer.msg("更新成功");
                            }
                        },
                        onError: function (error) {
                            var res = JSON.parse(error);
                            layer.msg(res.message)
                        }
                    });

                }
            });
        });
        $.ajaxSetup({async: false});
        initAreaSelect("add_province", "add_city", "district");
        initAreaSelect("receive_add_province", "receive_add_city", "receive_add_district");
        initShield("add_deliveryType");
        initOrg("add_partner");

        //$("#add_channel option[value='"+row[0].topChannelCode+"']").attr("selected", true);

        //根据receiveAreaName初始化select下拉框
        // var name = row[0].receiveAreaName.split("-");
        var deliveryNames = row[0].deliveryAreaName.split("-");
        $('#add_province option').filter(function () {
            return $(this).html() == deliveryNames[0]; }).attr("selected", true);
        $('#add_province').trigger("change");
        $('#add_city option').filter(function () {
            return $(this).html() == deliveryNames[1]; }).attr("selected", true);

        $("#add_deliveryType option[value='"+row[0].deliveryType+"']").attr("selected", true);
        $("#add_partner option[value='"+row[0].deliveryPartner+"']").attr("selected", true);


        var receiveNames = row[0].receiveAreaName.split("-");
        if(receiveNames.length==2){
            $('#receive_add_province option').filter(function () {
                return $(this).html() == receiveNames[0]; }).attr("selected", true);
            $('#receive_add_province').trigger("change");
        }

        if(receiveNames.length==3){
            $('#receive_add_province option').filter(function () {
                return $(this).html() == receiveNames[0]; }).attr("selected", true);
            $('#receive_add_province').trigger("change");
            $('#receive_add_city option').filter(function () {
                return $(this).html() == receiveNames[1]; }).attr("selected", true);
            $('#receive_add_city').trigger("change");
            if(receiveNames[2]=="*"){

            }else{
                $('#receive_add_district option').filter(function () {
                    return $(this).html() == receiveNames[2]; }).attr("selected", true);
            }
        }
    });


};




//删除操作
$.openx.delete = function () {
    $.openx.onClickDelete(function (row) {
    	var ids = $.openx.getArrays('id',row);
        $X("/logistics/logisticsDeliveryManager", "deleteDelivery").callx({
            ids: ids,
            onResult: function (data) {
                $.openx.tableRefresh(null, null, true);
                layer.msg("删除成功");
            },
            onError: function (error) {
                layer.msg("系统错误，请联系管理员");
            }
        });
    });
};


$.openx.query = function () {
    if($("#province option:selected").val()){
        var provinceName = $("#province option:selected").text();
    }
    if($("#city option:selected").val()){
        var cityName = $("#city option:selected").text();
    }else{
        var cityName="";
    }
    if($("#province option:selected").val()||$("#city option:selected").val()){
        $("#deliveryAreaName").val(provinceName+"-"+cityName)
    }else{
        $("#deliveryAreaName").val("");
    }

    if($("#receive_province option:selected").val()){
        var d_provinceName = $("#receive_province option:selected").text();
    }else{
        var d_provinceName="";
    }

    if($("#receive_city option:selected").val()){
        var d_cityName = $("#receive_city option:selected").text();
    }else{
        var d_cityName="";
    }
    if($("#receive_district option:selected").val()){
        var d_districtName = $("#receive_district option:selected").text();
    }else{
        var d_districtName="";
    }

    if($("#receive_district option:selected").val()){
        $("#receiveAreaName").val(d_provinceName+"-"+d_cityName+"-"+d_districtName);
    }else{
        if($("#receive_province option:selected").val()||$("#receive_city option:selected").val() ){
            $("#receiveAreaName").val(d_provinceName+"-"+d_cityName)
        }else{
            $("#receiveAreaName").val("");
        }
    }


    var data = $.openx.getJsonObj(queryForm);
    param.bean = data;
    $.openx.tableRefresh(url, param);

}